<?php

namespace Application\Site1Bundle\Controller;

use Symfony\Component\EventDispatcher;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Symfony\Component\EventDispatcher\Event;

use Doctrine\ORM\QueryBuilder;

use Bundle\TreeBundle\TreeManager;
use Bundle\TreeBundle\Entities\TreeDescription;
use Bundle\TreeBundle\Entities\Tree;

use Bundle\AclBundle\Acl;
use Bundle\AclBundle\Role as AclRole;
use Bundle\AclBundle\Resource as AclResource;

use Application\Site1Bundle\Extensions\TreeAclAssertion;

use Application\Site1Bundle\FireIceController;

class SitetreeController extends Controller
{
    public function indexAction()
    {
        return $this->render('Site1Bundle:Sitetree:index');
    }

    private function buildSitemap($trees)
    {
        $sitemap = array();

        $sitemap = \Bundle\TreeBundle\TreeManager::buildForest($trees);

        return $sitemap;
    }

    /**
     * @return Responce Отображение построенного дерева
     */
    public function listAction()
    {
        $em = $this['doctrine.orm.default_entity_manager'];
        $trees_repo = $em->getRepository('Bundle\TreeBundle\Entities\Tree');
        $trees = $trees_repo->getSiteTree();

        //$sitemap = $this->buildSitemap($trees);

        return $this->render('Site1Bundle:Sitetree:list', array('trees' => $trees));
    }

    public function editAction($id)
    {
        $em = $this['doctrine.orm.default_entity_manager'];
        $trees_repo = $em->getRepository('Bundle\TreeBundle\Entities\Tree');
        $tree = $trees_repo->find($id);

        $modules = $this['fireice']->getModuleManager()->getModules();

        $request = $this['request'];
        if($request->getMethod() == 'POST')
        {
            $tree->setTitle($request->request->get('title'));
            $tree->setOrder((int)$request->request->get('order'));

            if($tree->getDescription() == null)
            {
                $tree->setDescription(new \Bundle\TreeBundle\Entities\TreeDescription());
            }

            if($request->request->get('module') == 'null')
            {
                $tree->getDescription()->setModule(null);
            }
            else
            {
                $tree->getDescription()->setModule($request->request->get('module'));
            }

            $tree->setPath($request->request->get('path'));

            $tree->setController($request->request->get('controller'));

            $em->persist($tree->getDescription());
            $em->persist($tree);
            $em->flush();
            
            $this['fireice']->notify(new EventDispatcher\Event($this, 'sitetree.page_modified', array('tree' => $tree)));
        }

        return $this->render('Site1Bundle:Sitetree:edit', array('tree' => $tree, 'modules' => $modules));
    }


    public function allAction()
    {
        //die(\var_dump($this['request']));

        return $this->createResponse('all');
    }

    public function deleteAction($id)
    {
    	$em = $this['doctrine.orm.default_entity_manager'];
        $trees_repo = $em->getRepository('Bundle\TreeBundle\Entities\Tree');
        $tree = $trees_repo->find($id);
        
        $em->remove($tree);
        $em->flush();
    	
    	$this['fireice']->notify(new EventDispatcher\Event($this, 'sitetree.page_removed', array()));
        return $this->createResponse('<html><head>
                </head><body onload="parent.document.getElementById(\'1\').contentDocument.location.reload(true);"></body></html>
            ');
    }

    public function createAction($id)
    {
        $request = $this['request'];

        $modules = $this['fireice']->getModuleManager()->getModules();

        if($request->getMethod() == 'POST')
        {
        	
            $tree = new \Bundle\TreeBundle\Entities\Tree();
            $tree->setTitle($request->request->get('title'));
            $tree->setOrder($request->request->get('order'));

            $description = new \Bundle\TreeBundle\Entities\TreeDescription();

            $description->setModule($request->request->get('module'));

            if($id != -1)
            {
                $parent = $this['doctrine.orm.default_entity_manager']->find(
                    'Bundle\TreeBundle\Entities\Tree', $id
                );

                $tree->setParent($parent);
            }

            $tree->setPath($request->request->get('path'));

            $tree->setController($request->request->get('controller'));

            $tree->setDescription($description);

            $this['doctrine.orm.default_entity_manager']->persist($description);
            $this['doctrine.orm.default_entity_manager']->persist($tree);
            $this['doctrine.orm.default_entity_manager']->flush();

            $this['fireice']->notify(new EventDispatcher\Event($this, 'sitetree.page_created', array('tree' => $tree)));
            
        }
        
        return $this->render('Site1Bundle:Sitetree:create', array('parent' => (int)$id, 'modules' => $modules));
    }
}
